<?php

namespace app\api\controller;

use app\admin\model\Banner;
use app\common\controller\Api;
use app\common\model\Category;
use app\common\model\Goods;
use app\common\model\GoodsAttr;
use app\common\model\NoticeList;
use app\common\model\User;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     * @param string $token   token,可不传，默认为空
     * @param int $page 页数，默认1
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1618800233",
    "data": {
    "banner_list": [轮播图
    {
    "id": 2,
    "image": "http://www.shop.com/uploads/20210407/b6f68bd8b7d6ec3f63dd69a932312106.png",
    "createtime": 1617757679
    },
    {
    "id": 1,
    "image": "http://www.shop.com/uploads/20210407/fadd7d0f797a117ce5ab3007d84f993f.png",
    "createtime": 1617757669
    }
    ],
    "notice_info": {消息。notice_info为空时则不显示
    "id": 1,
    "title": "这是个测试标题"
    },
    "category_list": [分类列表
    {
    "id": 2,分类ID
    "image": "http://www.shop.com/uploads/20210406/5e39c61de0d8e525e582211c86469fc8.png",图片
    "name": "熟食类",名称
    "createtime": 1617713833
    },
    {
    "id": 1,
    "image": "http://www.shop.com/uploads/20210406/5be5ea2c9ab7dedd4cdce5d09ad82c5b.png",
    "name": "生鲜类",
    "createtime": 1617713822
    }
    ],
    "goods_list": [商品
    {
    "id": 1,商品ID
    "images": "http://www.shop.com/uploads/20210406/998bcbc925dd59eb924d7a100b9c0d1f.png",图片
    "name": "香辣酱",名称
    "price": "25.00",价格
    "is_recommend": 1,0不推荐1推荐
    "attr_name": "50g",规格
    "attr_id": 16,规格ID
    "status_text": ""
    },
    {
    "id": 3,
    "images": "http://www.shop.com/uploads/20210406/998bcbc925dd59eb924d7a100b9c0d1f.png",
    "name": "香辣酱2",
    "price": "40.00",
    "is_recommend": 1,
    "attr_name": "330ml",
    "status_text": ""
    },
    {
    "id": 2,
    "images": "http://www.shop.com/uploads/20210406/998bcbc925dd59eb924d7a100b9c0d1f.png",
    "name": "香辣酱1",
    "price": "25.00",
    "is_recommend": 0,
    "attr_name": "1000ml",
    "status_text": ""
    },
    {
    "id": 4,
    "images": "http://www.shop.com/uploads/20210406/998bcbc925dd59eb924d7a100b9c0d1f.png",
    "name": "香辣酱3",
    "price": "80.00",
    "is_recommend": 0,
    "attr_name": "500ml",
    "status_text": ""
    }
    ]
    }
    })
     */
    public function index()
    {
        $pagesize = 10;
        $page  = $this->request->get('page')==null?1:$this->request->get('page');
        $page = ($page-1)*$pagesize;
        $token = $this->request->get('token');
        //轮播图
        $banner_list = Banner::order('id desc')->select();
        foreach($banner_list as &$value){
            $value['image'] = $this->domain.$value['image'];
        }
        //消息
        $notice_info = array();
        if($token){
            $user = User::where('token',$token)->field('id,group_id')->find();
            if($user){
                $notice_info = NoticeList::where('group_id',$user['group_id'])->whereOr('group_id',0)->order('createtime desc')->field('id,title')->find();
            }
        }
        //分类
        $category_list = Category::order('id desc')->limit(10)->select();
        foreach($category_list as &$value){
            $value['image'] = $this->domain.$value['image'];
        }
        //推荐商品
        $goods_list = Goods::where('status',0)->field('id,images,name,price,is_recommend')->limit($page,$pagesize)->order('recommendtime desc')->select();
        foreach($goods_list as &$value){
            $images = explode(',',$value['images']);
            $value['images'] = $this->domain.$images[0];
            $attr_info = GoodsAttr::where('goods_id',$value['id'])->order('id asc')->field('id,attr_name')->find();
            $value['attr_name'] = $attr_info['attr_name'];
            $value['attr_id'] = $attr_info['id'];
        }
        $data = [
            'banner_list'=>$banner_list,
            'notice_info'=>$notice_info,
            'category_list'=>$category_list,
            'goods_list'=>$goods_list,
        ];
        $this->success($data);
    }
    /**
     * 搜索列表
     * @param int $search 搜索内容
     * @param int $page 页数，默认1
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1618800515",
    "data": [
    {
    "id": 1,商品ID
    "images": "http://www.shop.com/uploads/20210406/998bcbc925dd59eb924d7a100b9c0d1f.png",图片
    "name": "香辣酱",名称
    "price": "25.00",价格
    "is_recommend": 1,0不推荐1推荐
    "attr_name": "50g",规格
    "status_text": ""
    },
    {
    "id": 3,
    "images": "http://www.shop.com/uploads/20210406/998bcbc925dd59eb924d7a100b9c0d1f.png",
    "name": "香辣酱2",
    "price": "40.00",
    "is_recommend": 1,
    "attr_name": "330ml",
    "status_text": ""
    },
    {
    "id": 2,
    "images": "http://www.shop.com/uploads/20210406/998bcbc925dd59eb924d7a100b9c0d1f.png",
    "name": "香辣酱1",
    "price": "25.00",
    "is_recommend": 0,
    "attr_name": "1000ml",
    "status_text": ""
    },
    {
    "id": 4,
    "images": "http://www.shop.com/uploads/20210406/998bcbc925dd59eb924d7a100b9c0d1f.png",
    "name": "香辣酱3",
    "price": "80.00",
    "is_recommend": 0,
    "attr_name": "500ml",
    "status_text": ""
    }
    ]
    })
     */
    public function search_list()
    {
        $pagesize = 10;
        $page  = $this->request->get('page')==null?1:$this->request->get('page');
        $page = ($page-1)*$pagesize;
        $search = $this->request->get('search');
        if(!$search){
            $this->error('请输入搜索内容');
        }
        //商品
        $goods_list = Goods::where('status',0)->where('name','like','%'.$search.'%')->field('id,images,name,price,is_recommend')->limit($page,$pagesize)->order('recommendtime desc')->select();
        foreach($goods_list as &$value){
            $images = explode(',',$value['images']);
            $value['images'] = $this->domain.$images[0];
            $attr_info = GoodsAttr::where('goods_id',$value['id'])->order('id asc')->field('id,attr_name')->find();
            $value['attr_name'] = $attr_info['attr_name'];
            $value['attr_id'] = $attr_info['id'];
        }
        $this->success($goods_list);
    }
    /**
     * 分类列表
     * @ApiReturn   (  "code": 1,
    "msg": "",
    "time": "1618800653",
    "data": [
    {
    "id": 2,分类ID
    "name": "熟食类"分类名称
    },
    {
    "id": 1,
    "name": "生鲜类"
    }
    ]
    })
     */
    public function category_list()
    {
        $list =   Category::field('id,name')->order('id desc')->select();
        $this->success($list);
    }
    /**
     * 分类商品列表
     * @param int $category_id 分类ID
     * @param int $page 页数，默认1
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1618800764",
    "data": [
    {
    "id": 1,商品ID
    "images": "http://www.shop.com/uploads/20210406/998bcbc925dd59eb924d7a100b9c0d1f.png",图片
    "name": "香辣酱",名称
    "price": "25.00",价格
    "is_recommend": 1,0不推荐1推荐
    "attr_name": "50g",规格
    "status_text": ""
    },
    {
    "id": 3,
    "images": "http://www.shop.com/uploads/20210406/998bcbc925dd59eb924d7a100b9c0d1f.png",
    "name": "香辣酱2",
    "price": "40.00",
    "is_recommend": 1,
    "attr_name": "330ml",
    "status_text": ""
    }
    ]
    })
     */
    public function goods_list()
    {
        $pagesize = 10;
        $page  = $this->request->get('page')==null?1:$this->request->get('page');
        $page = ($page-1)*$pagesize;
        $category_id = $this->request->get('category_id');
        if(!$category_id){
            $this->error('请选择分类');
        }
        //商品
        $goods_list = Goods::where('status',0)->where('category_id',$category_id)->field('id,images,name,price,is_recommend')->limit($page,$pagesize)->order('recommendtime desc')->select();
        foreach($goods_list as &$value){
            $images = explode(',',$value['images']);
            $value['images'] = $this->domain.$images[0];
            $attr_info = GoodsAttr::where('goods_id',$value['id'])->order('id asc')->field('id,attr_name')->find();
            $value['attr_name'] = $attr_info['attr_name'];
            $value['attr_id'] = $attr_info['id'];
        }
        $this->success($goods_list);
    }
}
